package com.lbh.cloudgallery.service.impl;

import com.lbh.cloudgallery.entity.CartItemEntity;
import com.lbh.cloudgallery.entity.OrderEntity;
import com.lbh.cloudgallery.entity.vo.SearchProductVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.ProductDao;
import com.lbh.cloudgallery.entity.ProductEntity;
import com.lbh.cloudgallery.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");

        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("name",key)
                    .or().like("author",key)
                    .or().like("museum",key)
                    .or().like("style",key);
        }

        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProductEntity> searchProduct(SearchProductVo searchProductVo) {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(searchProductVo.getName())){
            wrapper.like("name",searchProductVo.getName());
        }
        if(!StringUtils.isEmpty(searchProductVo.getAuthor())){
            wrapper.like("author",searchProductVo.getAuthor());
        }
        if(!StringUtils.isEmpty(searchProductVo.getStyle())){
            wrapper.eq("style",searchProductVo.getStyle());
        }
        if(searchProductVo.getMinPrice() != null){
            wrapper.ge("price",searchProductVo.getMinPrice());
        }
        if(searchProductVo.getMaxPrice() != null){
            wrapper.le("price",searchProductVo.getMaxPrice());
        }
        List<ProductEntity> list = this.list(wrapper);
        return list;

    }

    @Override
    public void deleteStock(Integer productId, Integer count) {
        ProductEntity productEntity = this.getById(productId);
        productEntity.setWare(productEntity.getWare()-count);
        this.baseMapper.updateById(productEntity);
    }

    @Override
    public String createFromCart(Integer productId, Integer count) {
        ProductEntity ware = this.getById(productId);
        if (ware != null) {
            Long stock = ware.getWare();
            if (stock > count) {
                //库存充足，加入订单
                this.deleteStock(productId, count);
                return "库存充足";
            } else return ware.getName() + "现库存不足，请择日购买。";
        } else {
            return "库存不足，请择日购买。";
        }
    }

    /**@Override
    public Boolean addtoStock(Long productId, Integer count) {

        ProductEntity productEntity = this.getOne(new QueryWrapper<ProductEntity>().eq("product_id", productId));

        productEntity.setSaleCount(productEntity.getSaleCount()+count);
        this.baseMapper.updateById(productEntity);
        return true;
    }*/
    /**
     *
     */

}